package cn.tedu.toa.management.sys.controller;


import cn.tedu.toa.management.commons.vo.Result;
import cn.tedu.toa.management.sys.entity.FKeyservices;
import cn.tedu.toa.management.sys.entity.FNews;
import cn.tedu.toa.management.sys.service.FNewsService;
import cn.tedu.toa.management.sys.vo.NewsVo;
import cn.tedu.toa.management.util.UserUtils;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 新闻 前端控制器
 * </p>
 *
 * @author huaqian
 * @since 2021-10-02
 */
@RestController
@RequestMapping("/f-news")
@Slf4j
public class FNewsController {
    @Value("${myconfig.page-size}")
    private Integer pageSize;

    @Autowired
    private FNewsService newsService;

    @GetMapping("/getNews")
    public List<FNews> getNews(){
        return newsService.getNews();
    }

    @GetMapping("/news")
    public NewsVo news(String title){
        return newsService.news(title);
    }

    @GetMapping("/findNewsList")
    public Result<PageInfo<FNews>> findNewsList(String name,
                                                              @RequestParam(defaultValue = "1") Integer pageNum){
        PageInfo<FNews> pageInfo=newsService.findNewsList(name,pageNum,pageSize);
        return Result.ok(pageInfo);
    }

    @GetMapping("/getNewsByName")
    public FNews getNewsByName(String title){
        return newsService.getNewsByTitle(title);
    }

    @PostMapping("/edit")
    public Result edit(FNews news){
        log.debug("修改的内容:{}",news);
        newsService.edit(news,UserUtils.getCurrentUserName());
        return Result.ok();
    }

    @PostMapping("/insert")
    public Result insert(FNews news){
        log.debug("新增的内容:{}",news);
        newsService.insert(news, UserUtils.getCurrentUserName());
        return Result.ok();
    }

    //删除
    @GetMapping("/delete")
    public Result delete(Integer id){
        log.debug("删除的内容id:{}",id);
        newsService.delete(id);
        return Result.ok();
    }
}
